package com.zert.filter;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet Filter implementation class Myfilter
 */
@WebFilter({ "/*" })
public class MyAccessFilter implements Filter {
	
	/**
	 * Default constructor.
	 */
	public MyAccessFilter() {
		System.out.println("构造方法");
	}

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		System.out.println("销毁方法");
	}

	/**
	 * @see 核心方法
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {

		int empid=0;
		// 获取地址
		HttpServletRequest rep = (HttpServletRequest) request;
		//HttpServletResponse res = (HttpServletResponse) response;
		// 得到想请求的地址
		//System.out.println(rep.getRequestURI());
		//System.out.println(rep.getRequestURL());
		// 获取uri地址
		String uri = rep.getRequestURI();
		Object object;
		// 如果在session中获取emp对象为Object
		Object hashmapObject = rep.getSession().getAttribute("emp");
		//判断得到session中的值是否为空 最后emp转为empid
		if (hashmapObject!=null) {
			System.out.println(hashmapObject);
			HashMap hashMap = (HashMap) hashmapObject;
			object = hashMap.get("empid");
			empid = (int) object;
		}
		// 请问是否让请求正常响应给客户,如果是请放行
		if (uri.endsWith("login.jsp") || uri.endsWith(".css") || uri.endsWith(".js") || uri.endsWith("EmpServlet")
				|| uri.endsWith("facelogin.jsp")||uri.endsWith("AttendanceServlet")|| uri.endsWith(".jpg")) {
			// 放行
			chain.doFilter(request, response);
		}else if (empid > 0) {
			// 放行
			chain.doFilter(request, response);
		}else{
			//转发回登录界面
			rep.getRequestDispatcher("login.jsp").forward(request, response);
		}
	}
//		int empid=0; 
//		// 获取地址
//		HttpServletRequest rep = (HttpServletRequest) request;
//		// HttpServletResponse res = (HttpServletResponse) response;
//		// 得到想请求的地址
//		//System.out.println(rep.getRequestURI());
//		//System.out.println(rep.getRequestURL());
//		// 获取uri地址
//		String uri = rep.getRequestURI();
//		Object object;
//		// 如果在session中获取emp对象为Object
//		Object hashmapObject = rep.getSession().getAttribute("emp");
		//判断得到session中的值是否为空 最后emp转为empid
//		if (hashmapObject!=null) {
//			System.out.println(hashmapObject);
//			HashMap hashMap = (HashMap) hashmapObject;
//			object = hashMap.get("empid");
//			empid = (int) object;
//		}
//		// 请问是否让请求正常响应给客户,如果是请放行
//		if (uri.endsWith("login.jsp") || uri.endsWith(".css") || uri.endsWith(".js") || uri.endsWith("EmpServlet")
//				|| uri.endsWith("facelogin.jsp")) {
//			// 放行
//			chain.doFilter(request, response);
//		}else if (empid > 0) {
//			// 放行
//			chain.doFilter(request, response);
//		}else {
//			//转发回登录界面
//			rep.getRequestDispatcher("login.jsp").forward(request, response);
//		}
//		
//		chain.doFilter(request, response);
//	}
	/**
	 * @see 初始化的方法
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		System.out.println("初始化的方法");
	}

}
